package com.gdougms.interceptor;

import com.gdougms.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.omg.PortableInterceptor.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@Component
public class UserGetOneInfoInterceptor implements HandlerInterceptor {
    @Autowired
    HttpSession httpSession;

    /**@Title
     * @Description 当对数据库的用户增删改查时，查看是否登录，登录之后的role是否符合操作角色
     * @Param [request, response, handler]
     * @return boolean
     * @Author iceF
     * @Date 2021/6/19
    */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User) httpSession.getAttribute("User");
        log.info("进入拦截器UserInterceptor"+user);
        //未登录
        if(user == null){
            log.info("退出拦截器UserInterceptor-----未登录");
            return false;
        }
        log.info("进入拦截器UserInterceptor-----已经登录");
        //登录的人没角色
        if(user.getRole() == null){
            return false;
        }
        log.info("进入拦截器UserInterceptor-----有角色");
        if(user.getRole() != 3 || user.getRole() != 4 || user.getIdCard().equals(request.getParameter("id")) ||user.getNumber().equals(request.getParameter("id"))){
            return true;
        }
        log.info("进入拦截器UserInterceptor-----通过拦截器");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}


